package com.foolrabbit.springbootlearning.event.service;

import com.foolrabbit.springbootlearning.event.entity.Baby;
import com.foolrabbit.springbootlearning.event.publisher.BabyCryPublisher;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class PlaygroundCenter {

    private final BabyCryPublisher babyCryPublisher;

    public PlaygroundCenter(BabyCryPublisher babyCryPublisher) {
        this.babyCryPublisher = babyCryPublisher;
    }

    private final List<Baby> babyList = new ArrayList<>();

    public void addBaby(Baby baby) {
        babyList.add(baby);
    }

    public void cry(){
        // 随机取一个baby哭
        Baby baby = babyList.get((int) (Math.random() * babyList.size()));
        babyCryPublisher.publish(baby);
    }
}
